package com.ruoyi.eims.purchase.service.impl;

import com.ruoyi.common.core.utils.StrUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eims.purchase.domain.PurchaseOrder;
import com.ruoyi.eims.purchase.domain.PurchaseOrderDetails;
import com.ruoyi.eims.purchase.domain.PurchasePay;
import com.ruoyi.eims.purchase.dto.GoodsDto;
import com.ruoyi.eims.purchase.dto.OrderDto;
import com.ruoyi.eims.purchase.mapper.PurchaseOrderDetailsMapper;
import com.ruoyi.eims.purchase.mapper.PurchaseOrderMapper;
import com.ruoyi.eims.purchase.mapper.PurchasePayMapper;
import com.ruoyi.eims.purchase.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购订单管理Service业务层处理
 * 
 * @author hp
 * @date 2023-02-04
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService
{
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private PurchaseOrderDetailsMapper purchaseOrderDetailsMapper;
    @Autowired
    private PurchasePayMapper purchasePayMapper;

    /**
     * 查询采购订单管理
     * 
     * @param id 采购订单管理主键
     * @return 采购订单管理
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询采购订单管理列表
     * 
     * @param purchaseOrder 采购订单管理
     * @return 采购订单管理
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增采购订单管理
     * 
     * @param orderDto 采购订单管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPurchaseOrder(OrderDto orderDto)
    {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setCreateDate(new Date());



        purchaseOrder.setDeposit(orderDto.getDepositAmount());
        purchaseOrder.setGoodsId(orderDto.getGoodsList().stream().map(t->String.valueOf(t.getId())).collect(Collectors.joining(",")));
        purchaseOrder.setOrderNo(StrUtils.getComplexRandomString(6));//todo
        purchaseOrder.setPrice(orderDto.getTotalAmount());//合计金额
        purchaseOrder.setState(orderDto.getState());
        purchaseOrder.setUserId(SecurityUtils.getUserId());
        purchaseOrder.setPreferentialPrice(orderDto.getDiscountAmount());
        purchaseOrder.setRealPrice(orderDto.getRealAmount());
        purchaseOrder.setVendorId(orderDto.getVendorId());

        int i = purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);

        if (orderDto.getDepositAmount() != null && !StringUtils.isEmpty(orderDto.getDepositAmount().toString())) {
            PurchasePay pay = new PurchasePay();
            pay.setAccountId(orderDto.getAccountId());
            pay.setCreatedate(new Date());
            pay.setNote("定金支付");
            pay.setPrice(orderDto.getDepositAmount());
            pay.setPurchaseOrderId(purchaseOrder.getId());
            pay.setOrderNo(purchaseOrder.getOrderNo());
            purchasePayMapper.insertPurchasePay(pay);
        }

        for (GoodsDto dto : orderDto.getGoodsList()) {
            PurchaseOrderDetails details = new PurchaseOrderDetails();
            details.setAccountId(orderDto.getAccountId());
            details.setGoodsId(dto.getId());
            details.setOrderId(purchaseOrder.getId());
            details.setPrice(dto.getCostPrice());
            details.setCount(dto.getNumber());
            details.setCountPrice(dto.getCostPrice().multiply(new BigDecimal(dto.getNumber())));

            purchaseOrderDetailsMapper.insertPurchaseOrderDetails(details);
        }



        //return purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);
        return i;
    }

    /**
     * 修改采购订单管理
     * 
     * @param purchaseOrder 采购订单管理
     * @return 结果
     */
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除采购订单管理
     * 
     * @param ids 需要删除的采购订单管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderByIds(Long[] ids)
    {
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除采购订单管理信息
     * 
     * @param id 采购订单管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }

    @Override
    public int shenghe(PurchaseOrder purchaseOrder) {

        purchaseOrder.setState(1);
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }
}
